// 引入react
import * as React from 'react';
import {StyleSheet, View, Text, ScrollView, TouchableOpacity} from 'react-native';
import {SafeAreaView} from 'react-native-safe-area-context';
import AllStyle from '../../../style';
import {HeaderTitle} from '../../../componet/settingComponent/HeaderTitle';
import Icon from 'react-native-vector-icons/AntDesign';
import {BrLine} from '../../../componet/settingComponent/BrLine';
import {CardFold} from '../../../componet/statisticsComponent/CardFold';
import {getActualMonth, getActualMonthByUserId} from '../../../api/empActual';
import {Loading} from '../../../utils/Loading';
import {Toast} from '../../../utils/Toast';

/**
 * @Description:
 * @author TangYong
 * @date 2023/2/7
 */
export class UserStatisticsDetails extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            statsNum:(!this.props.route.params.statsNum)?0:this.props.route.params.statsNum,
            userId:(!this.props.route.params.userId)?null:this.props.route.params.userId,
            userMonthStatisticsData: {
                average:0,
                attendanceDataList:[],
                earlyDataList:[],
                beLateDataList:[],
                lackDataList:[],
                absenteeismDataList:[],
                restDataList:[],
            },
        };
    }

    //生命周期：组件挂载后
    componentDidMount() {
        //alert()
        this.getMonth();
        //console.log(this.props.navigation)
    }
    componentDidUpdate(oldProps) {
        // 比较前后props
        if (this.props.route.params != oldProps.route.params) {
            //this.getMonth();
            this.setState({statsNum:this.props.route.params.statsNum})
            //alert(this.props.route.params.statsNum,)

        }
        //console.log(this.props)

    }


    //方法
    back = () => {
        this.props.navigation.goBack();
    };
    getMonth() {

        if (!this.props.route.params.userId){
            Loading.show();
            getActualMonth().then(res => {
                Loading.hidden();
                let userMonthStatisticsData =res.data;
                this.setState({userMonthStatisticsData})
                //console.log(res);
            }).catch(err => {
                Toast.showWarning(err.toString())
                Loading.hidden();

            }).finally(()=>{
                Loading.hidden();
            });
        }else {
            Loading.show();
           let userId=this.props.route.params.userId
            getActualMonthByUserId(userId).then(res => {
                Loading.hidden();
                let userMonthStatisticsData =res.data;
                this.setState({userMonthStatisticsData})
                //console.log(res);
            }).catch(err => {
                Loading.hidden();
            }).finally(()=>{
                Loading.hidden();
            });
        }

    }
    goto = (name, params) => {

        const route = {
            name: name,
            params: {
                //routePathName: this.props.route.name,
                ...params,
            },

        };
        this.props.navigation.navigate(route);
    };

    //DOM
    render() {
        return (
            <SafeAreaView edges={['top', 'left', 'right', 'bottom']} style={{flex: 1, backgroundColor: '#FFF'}}>
                <View style={AllStyle.container}>
                    <HeaderTitle goBack={() => {
                        this.back();
                    }} headtitle={'个人月详情统计'}
                    />
                    <ScrollView style={AllStyle.padding15}>
                        {/*<Text>{this.state.statsNum}</Text>*/}
                        <View style={AllStyle.card}>
                            <CardFold  userId={this.state.userId} navigation={this.props.navigation} itemNum={0} title={'平均工时：'+this.state.userMonthStatisticsData.average+'小时'} ItemList={[]} ></CardFold>
                        </View>
                        <View style={AllStyle.card}>
                            <CardFold stats={this.state.statsNum==1?true:false} userId={this.state.userId} navigation={this.props.navigation}  itemNum={this.state.userMonthStatisticsData.attendanceDataList.length} title={'出勤'+this.state.userMonthStatisticsData.attendanceDataList.length+'天'} ItemList={this.state.userMonthStatisticsData.attendanceDataList} ></CardFold>
                        </View>
                        <View style={AllStyle.card}>
                            <CardFold  stats={this.state.statsNum==6?true:false} userId={this.state.userId}  navigation={this.props.navigation} itemNum={this.state.userMonthStatisticsData.restDataList.length} title={'休息'+this.state.userMonthStatisticsData.restDataList.length+'天'} ItemList={this.state.userMonthStatisticsData.restDataList} ></CardFold>
                        </View>
                        <View style={AllStyle.card}>
                            <CardFold stats={this.state.statsNum==4?true:false} userId={this.state.userId} navigation={this.props.navigation} itemNum={this.state.userMonthStatisticsData.beLateDataList.length} title={'迟到'+this.state.userMonthStatisticsData.beLateDataList.length+'天'} ItemList={this.state.userMonthStatisticsData.beLateDataList} ></CardFold>
                        </View>
                        <View style={AllStyle.card}>
                            <CardFold stats={this.state.statsNum==5?true:false} userId={this.state.userId} navigation={this.props.navigation} itemNum={this.state.userMonthStatisticsData.earlyDataList.length} title={'早退'+this.state.userMonthStatisticsData.earlyDataList.length+'天'} ItemList={this.state.userMonthStatisticsData.earlyDataList} ></CardFold>
                        </View>
                        <View style={AllStyle.card}>
                            <CardFold stats={this.state.statsNum==3?true:false} userId={this.state.userId} navigation={this.props.navigation} itemNum={this.state.userMonthStatisticsData.lackDataList.length} title={'缺卡'+this.state.userMonthStatisticsData.lackDataList.length+'天'} ItemList={this.state.userMonthStatisticsData.lackDataList} ></CardFold>
                        </View>
                        <View style={AllStyle.card}>
                            <CardFold stats={this.state.statsNum==2?true:false}  userId={this.state.userId} navigation={this.props.navigation}  itemNum={this.state.userMonthStatisticsData.absenteeismDataList.length} title={'旷工'+this.state.userMonthStatisticsData.absenteeismDataList.length+'天'} ItemList={this.state.userMonthStatisticsData.absenteeismDataList} ></CardFold>
                        </View>

                    </ScrollView>
                </View>
            </SafeAreaView>
        );
    }

}

const style = StyleSheet.create({

});

